package com.weapons.week0903_weapons.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.weapons.week0903_weapons.R;
import com.weapons.week0903_weapons.bean.RecommentContentBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/7/23.
 */
public class RecommendContentActivity extends AppCompatActivity {
    private TextView tv_content_title, tv_content_time;
    private RelativeLayout rl_progressbar;
    private LinearLayout ll_content_group;
    private List<String> mContents;
    private List<String> mImgsUrl;
    private ImageView iv_back;
    private Button btn_zan;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recommend_content);
        mContents = new ArrayList<>();
        mImgsUrl = new ArrayList<>();
        tv_content_time = (TextView) findViewById(R.id.tv_content_time);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        rl_progressbar = (RelativeLayout) findViewById(R.id.rl_progressbar);
        ll_content_group = (LinearLayout) findViewById(R.id.ll_content_group);
        iv_back= (ImageView) findViewById(R.id.iv_back);
        btn_zan= (Button) findViewById(R.id.btn_zan);
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        getData(url);
    }

    private void getData(String url) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                System.out.println("aaaaa" + result);
                String str = "{" + ((result.split("\\{"))[2].split("\\}"))[0] + "}";
                final RecommentContentBean recommentContentBean = new Gson().fromJson(str, RecommentContentBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mImgsUrl.addAll(recommentContentBean.getPics());
                        mContents.addAll(recommentContentBean.getContent());
                        tv_content_time.setText(recommentContentBean.getPubDate());
                        tv_content_title.setText(recommentContentBean.getTitle());
                        rl_progressbar.setVisibility(View.GONE);
                        btn_zan.setText(recommentContentBean.getGoodpost());
                        getInitContentView();

                    }
                });
            }
        });
    }

    private void getInitContentView() {
        for (int i = 0; i < mContents.size(); i++) {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(15, 15, 15,15);
            String content = mContents.get(i).replace("\u3000\u3000\u3000\u3000", "\n\u3000\u3000");
            TextView tv = new TextView(this);
            tv.setTextSize(16);
            tv.setLineSpacing(8, 1);
            tv.setLayoutParams(layoutParams);
            tv.setText(content);
            ll_content_group.addView(tv);
            ImageView iv = new ImageView(this);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setLayoutParams(layoutParams);
            System.out.println("aaaaaaaaaa" + mImgsUrl.get(i));
            Picasso.with(this).load(mImgsUrl.get(i)).into(iv);
            ll_content_group.addView(iv);
        }
    }

    public void btn_back(View view){
        finish();
    }

    public void btn_zan(View view){

    }
}
